package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 通用接口
 */
@RequestMapping("/admin/common")
@RestController
@Api(tags = "通用接口")
@Slf4j
public class ComonController {

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    //TODO 阿里云文件上传（新增菜品）
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        log.info("文件上传{}", file);

        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFIleName = UUID.randomUUID().toString() + extName;
        log.info("新文件名{}", newFIleName);

        file.transferTo(new File("D:\\sky\\nginx-1.20.2\\aimg\\" + newFIleName));

        String fileUrl = "http://localhost:80/img/" + newFIleName;
        return Result.success(fileUrl);
    }



}
